﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Auxiliar;
using BLL;
using System.IO;

namespace HRP.Private.Funcionarios.Controlos
{
    public partial class Ficha_Agenda : System.Web.UI.UserControl
    {

        private string _NumFuncionario
        {
            get
            {
                if (ViewState["NumFuncionario"] != null)
                {
                    return (string)ViewState["NumFuncionario"];
                }
                return string.Empty;
            }
            set
            {
                ViewState["NumFuncionario"] = value;
            }
        }

        public string NumFuncionario
        {
            get
            {
                return _NumFuncionario;
            }
            set
            {
                _NumFuncionario = value;
            }
        }

        public Funcionario FuncionarioActual { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            Page.PreRender += Page_PreRender;
        }

        void Page_PreRender(object sender, EventArgs e)
        {
            if (FuncionarioActual != null)
            {
                CarregarFaltas();
                CarregarHorasExtras();
                if(!Page.IsPostBack)
                {
                    NumFuncionario = FuncionarioActual.Dados.id.ToString();
                }
            }

        }

        private void CarregarHorasExtras()
        {
            horasExtrasTable.DataSource = FuncionarioActual.HorasExtras;
            horasExtrasTable.DataBind();
        }

        private void CarregarFaltas()
        {
            faltasTable.DataSource = FuncionarioActual.Faltas;
            faltasTable.DataBind();
        }

        public void alterarEstadoFunc(Object sender, CommandEventArgs e)
        {
            int itemId = 0;
            Int32.TryParse(e.CommandArgument.ToString(), out itemId);
            if (itemId != 0)
            {
                //TODO: criar log
                if (e.CommandName == "F")
                {
                    Falta_BLL.Instance.Activar_DesactivarFaltas(itemId);
                }
                if (e.CommandName == "HE")
                {
                    HorasExtras_BLL.Instance.Activar_DesactivarHorasExtra(itemId);
                }

            }
        }

        public void downloadFaltaFunc(Object sender, CommandEventArgs e)
        {
            string url = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(url))
            {
                DownloadFile(url);
            }
        }

        public void DownloadFile(string url)
        {
            try
            {
                string finalUrl = Server.MapPath(url);

                if (File.Exists(finalUrl))
                {
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    FileInfo f = new FileInfo(finalUrl);
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + f.Name);
                    Response.AddHeader("Content-Length", f.Length.ToString());
                    Response.ContentType = "text/plain";
                    //Response.Flush();
                    Response.WriteFile(url);
                    Response.End();

                }
            }
            catch (Exception ex)
            {
                return;
                //  throw ex;
            }
        }

    }
}